package cate.common.table.activity.happyflop.sheet;

import cate.common.table.ESheet;
import cate.common.table.activity.happyflop.row.HappyFlopRwdPoolRow;
import cate.common.util.random.RWList;
import cate.common.util.random.SimpleRwItem;
import easy.java.dev.note.NoteClass;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass("翻翻乐-奖池 Sheet")
public class HappyFlopRwdPoolSheet extends ESheet<HappyFlopRwdPoolRow> {
    @Override
    public String getSheetName() {
        return "奖池";
    }

    private Map<Integer, List<HappyFlopRwdPoolRow>> poolMap = new HashMap<>();

    @Override
    protected void onLoaded() {
        poolMap.clear();
        for (HappyFlopRwdPoolRow row : this.getList()) {
            List<HappyFlopRwdPoolRow> list = poolMap.computeIfAbsent(row.pool, k -> new ArrayList<>());
            list.add(row);
        }
    }

    public int randomRwd(int pool) {
        List<HappyFlopRwdPoolRow> list = poolMap.get(pool);
        if (list == null) {
            return -1;
        }
        RWList<SimpleRwItem> rwList = new RWList<>();
        for (HappyFlopRwdPoolRow row : list) {
            rwList.add(new SimpleRwItem(row.id, row.weight));
        }
        rwList.setup();
        SimpleRwItem simpleRwItem = rwList.get();
        if (simpleRwItem == null) {
            return -1;
        }
        return simpleRwItem.getId();
    }
}
